package cn.itsource.pet.controller;

import cn.itsource.basic.dto.PetDto;
import cn.itsource.pet.service.IPetService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    public IPetService petService;
    /**
     * 接口：添加或修改
     *
     * @param petDto 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody PetDto petDto) {
        if (petDto.getId() != null) {
            try {
                petService.update(petDto);
            }catch (Exception e){
                return AjaxResult.me().setSuccess(false).setMessage("无法修改上架商品");
            }
        } else {
            petService.insert(petDto);
        }
        return new AjaxResult();

    }
    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        petService.delete(id);
        return new AjaxResult();
    }
    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping("/patchDelete")
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        petService.patchDelete(ids);
        return new AjaxResult();
    }
    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/selectById/{id}")
    public Pet findOne(@PathVariable("id") Long id) {
        return petService.selectById(id);
    }
    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping("/selectAll")
    public List<Pet> findAll() {
        return petService.selectAll();
    }
    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/selectByQueryPage")
    public AjaxResult queryPage(@RequestBody PetQuery query) {
        System.out.println(query);
        return new AjaxResult().setResultObj(petService.selectByQueryPage(query));
    }

    @PostMapping("/selectByQueryPage2")
    public AjaxResult queryPage2(@RequestBody PetQuery query) {
        System.out.println(query);
        return new AjaxResult().setResultObj(petService.selectByQueryPage2(query));
    }

    @PatchMapping("/onShelves")
    public AjaxResult onShelves(@RequestBody List<Long> ids) {
        petService.onShelves(ids);
        return new AjaxResult();
    }

    @PatchMapping("/offShelves")
    public AjaxResult offShelves(@RequestBody List<Long> ids) {
        petService.offShelves(ids);
        return new AjaxResult();
    }
}
